# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=serf
pkgname=("lib${pkgbase}" "lib${pkgbase}-devel")
pkgver=1.3.9
pkgrel=2
pkgdesc="High-performance asynchronous HTTP client library"
arch=('i686' 'x86_64')
url="https://serf.apache.org/"
license=('Apache')
makedepends=('scons' 'apr-util-devel' 'heimdal-devel' 'zlib-devel' 'openssl-devel' 'libiconv-devel')
source=(https://archive.apache.org/dist/serf/${pkgbase}-${pkgver}.tar.bz2
        03-destdir.patch
        05-disable-SHLIBVERSION.patch
        06-strcasecmp.patch
        07-sspi-auth.patch)
sha256sums=('549c2d21c577a8a9c0450facb5cca809f26591f048e466552240947bdf7a87cc'
            '87767cd9b5e568a10f234df46f413a408613965e9d1b8cde1c0145f7eb20f0f1'
            '1efab80c71b6987ed504d307c39a623aef849bc7cc39c6aeac3d9338f7459be8'
            '313b272609729d80dedf608f9f1939526e687fdf4a1b50e1a66505c93c2ee56f'
            '64fcbd8ed1c3c5d76c0deeff5e41bc73c2354276584638ce3e5cd86ef0d9f4b5')

prepare() {
  cd ${pkgbase}-${pkgver}

  patch -p1 -i ${srcdir}/03-destdir.patch
  patch -p1 -i ${srcdir}/05-disable-SHLIBVERSION.patch
  patch -p1 -i ${srcdir}/06-strcasecmp.patch
  patch -p1 -i ${srcdir}/07-sspi-auth.patch
}

build() {
  cd ${pkgbase}-${pkgver}
  scons PREFIX=/usr \
        GSSAPI=/usr/bin/krb5-config \
        CFLAGS="${CFLAGS} -D_XOPEN_SOURCE=700"

  install -d "${srcdir}/dest/usr"
  scons DESTDIR="${srcdir}/dest" install

  versions=($(echo ${pkgver} | tr -t '.' '\n'))
  major=${versions[0]}
  minor=${versions[1]}

  # Rename the libraries
  mv ${srcdir}/dest/usr/lib/libserf-${major}-0.dll.a ${srcdir}/dest/usr/lib/libserf-${major}.dll.a
  mv ${srcdir}/dest/usr/lib/libserf-${major}-0.a ${srcdir}/dest/usr/lib/libserf-${major}.a

  # Install the DLL
  rm ${srcdir}/dest/usr/lib/msys-serf-${major}-0.dll
  install -Dm755 msys-serf-${major}-0.dll ${srcdir}/dest/usr/bin/msys-serf-${major}-0.dll
}

#check() {
#  cd ${pkgbase}-${pkgver}
#  scons check
#}

package_libserf() {
  depends=('apr-util')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libserf-devel() {
  pkgdesc="Libserf headers and libraries"
  groups=('development')
  depends=("libserf=${pkgver}" 'apr-util-devel')
  options=('staticlibs')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
